package com.lottery.im.module.chat.ui;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.MemberVipInfo;
import com.lottery.im.api.result.entity.SectionMemberVipInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.chat.adapter.SelectMemberVipAdapter;
import com.lottery.im.module.setting.ui.ProfileFragment;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 群成员VIP搜索
 */
public class MemberVipSearchFragment extends BaseSwipeFragment {

  private final static String INFO_KEY = "INFO_KEY";

  public static MemberVipSearchFragment create(List<SectionMemberVipInfo> memberVipInfoList) {
    MemberVipSearchFragment fragment = new MemberVipSearchFragment();
    Bundle b = new Bundle();
    b.putSerializable(INFO_KEY, (Serializable) memberVipInfoList);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.et_search) EditText mEtSearch;
  @BindView(R.id.iv_clear) ImageView mIvClear;
  @BindView(R.id.list) RecyclerView mList;
  @BindView(R.id.tv_empty_tip) TextView mTvEmptyTip;

  private List<SectionMemberVipInfo> mShowMemberVipInfoList;
  private List<SectionMemberVipInfo> mMemberVipInfoList;
  private SelectMemberVipAdapter mAdapter;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_member_vip_search;
  }

  @Override protected void initPresenter() {
    initView();
  }

  private void initView() {

    mMemberVipInfoList = (List<SectionMemberVipInfo>) getArguments().getSerializable(INFO_KEY);
    mShowMemberVipInfoList = new ArrayList<>();
    mList.setLayoutManager(new LinearLayoutManager(mActivity));
    RecyclerViewDivider.with(mActivity)
        .color(ContextCompat.getColor(mActivity, R.color.windowBackground))
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider()
        .build()
        .addTo(mList);
    mAdapter = new SelectMemberVipAdapter(mShowMemberVipInfoList);
    mAdapter.setOnItemClickListener((adapter, view, position) -> {

      if (mShowMemberVipInfoList.get(position).isHeader) return;

      MemberVipInfo info = mShowMemberVipInfoList.get(position).t;
      if (info.getId().equals(MyApplication.getInstance().getThirdAccId())) {
        startFragment(new ProfileFragment());
      } else {

      }
    });
    mList.setAdapter(mAdapter);
  }

  @OnClick({ R.id.iv_clear, R.id.tv_cancel }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_clear:
        mEtSearch.setText("");
        break;
      case R.id.tv_cancel:
        popBackStack();
        break;
    }
  }

  @OnTextChanged(value = R.id.et_search, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
  void afterTextChanged(Editable content) {
    if (TextUtils.isEmpty(content.toString())) {
      mIvClear.setVisibility(View.GONE);
    } else {
      mIvClear.setVisibility(View.VISIBLE);
    }
    search(content.toString());
  }

  /**
   * 群成员搜索
   *
   * @param content 搜索内容
   */
  private void search(String content) {
    mShowMemberVipInfoList.clear();
    if (TextUtils.isEmpty(content)) {
      mTvEmptyTip.setVisibility(View.GONE);
      mList.setVisibility(View.VISIBLE);
    } else {
      for (SectionMemberVipInfo info : mMemberVipInfoList) {
        if (info.t.getName().contains(content)) {
          mShowMemberVipInfoList.add(info);
        }
      }
      if (mShowMemberVipInfoList.size() == 0) {
        mTvEmptyTip.setVisibility(View.VISIBLE);
        mList.setVisibility(View.GONE);
      } else {
        mTvEmptyTip.setVisibility(View.GONE);
        mList.setVisibility(View.VISIBLE);
      }
    }
    mAdapter.notifyDataSetChanged();
  }
}
